﻿#region Copyright information
// 
// Copyright © 2012-2013 Yongkee Cho. All rights reserved.
// 
// This code is a part of the SubnetworkToolkit and governed under the terms of the
// GNU Lesser General  Public License (LGPL) version 2.1 which accompanies this distribution.
// For more information on the LGPL, please visit http://bol.codeplex.com/license.
// 
// - Filename: Program.cs
// - Author: Yongkee Cho
// - Email: yongkeecho@outlook.com
// - Date Created: 2013-01-30 2:55 PM
// - Last Modified: 2013-01-30 3:21 PM
// 
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using CommandLine;
using SubnetworkToolkit.Data;
using SubnetworkToolkit.IO;

namespace SubnetworkToolkit.Convert
{
    static class Program
    {
        static void Main(string[] args)
        {
            var options = new Options();

            try
            {
                // parse arguments
                var parser = new CommandLineParser(new CommandLineParserSettings(Console.Error));
                parser.ParseArguments(args, options);

                if (args.Length == 0 || args[0] == "-h")
                    Console.Error.Write(options.GetUsage());
                else
                {
                    var mode = options.Mode;
                    var inputFile = options.InputFile;
                    var outputFile = options.OutputFile;
                    var annotations = new AnnotationsHuman();

                    // reads include file
                    var gr = new GmtReader(inputFile);
                    var geneSets = gr.Read();
                    gr.Close();

                    if (mode == 0)
                    {
                        var symbol2Id = annotations.Symbol2Id.ToDictionary(x => x.Symbol, x => x.Id);

                        foreach (var geneSet in geneSets)
                        {
                            var genes = new List<Gene>();
                            foreach (var gene in geneSet.Genes)
                            {
                                var symbol = gene.Symbol;
                                if (symbol2Id.ContainsKey(symbol))
                                {
                                    gene.Id = symbol2Id[symbol];
                                    genes.Add(gene);
                                }
                            }
                            geneSet.Genes = genes;
                        }
                    }
                    else
                    {
                        var id2Symbol = annotations.Symbol2Id.ToList();

                        foreach (var geneSet in geneSets)
                        {
                            var genes = new List<Gene>();
                            foreach (var gene in geneSet.Genes)
                            {
                                var id = gene.Id;
                                if (id2Symbol.Any(x => x.Id.Equals(id)))
                                {
                                    gene.Symbol = id2Symbol.First(x => x.Id.Equals(id)).Symbol;
                                    genes.Add(gene);
                                }
                            }
                            geneSet.Genes = genes;
                        }
                    }

                    var gw = new GmtWriter(outputFile, geneSets, true);
                    gw.Write();
                    gw.Close();
                }
            }
            catch (Exception e)
            {
                Console.Error.Write(e.Message);
            }
        }
    }
}
